package com.casic.modules.sys.service.impl;


import com.casic.modules.sys.dao.SysUserRoleDao;
import com.casic.modules.sys.entity.SysUserRoleEntity;
import com.casic.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 用户与角色对应关系
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:45:48
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
	@Autowired
	private SysUserRoleDao sysUserRoleDao;

	/** 生成主键策略 */
	public String createId() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	@Override
	public void saveOrUpdate(String userId, List<String> roleIdList) {
		if(roleIdList.size() == 0){
			return ;
		}
		if(userId!=null){
			//先删除用户与角色关系
			sysUserRoleDao.delete(userId);
		}
		List<SysUserRoleEntity> dataList=new ArrayList<>();
		SysUserRoleEntity sysUserRoleEntity=null;
		for(String roleId:roleIdList){
			sysUserRoleEntity=new SysUserRoleEntity();
			sysUserRoleEntity.setId(createId());
			sysUserRoleEntity.setRoleId(roleId);
			sysUserRoleEntity.setUserId(userId);
			dataList.add(sysUserRoleEntity);
		}
		//保存用户与角色关系
		Map<String, Object> map = new HashMap<>();
		map.put("roleIdList", dataList);
		sysUserRoleDao.save(map);
	}

	@Override
	public List<String> queryRoleIdList(String userId) {
		return sysUserRoleDao.queryRoleIdList(userId);
	}

	@Override
	public void delete(String userId) {
		sysUserRoleDao.delete(userId);
	}

	@Override
	public void deleteBatch(String[] userId) {
		sysUserRoleDao.deleteBatch(userId);
	}

	@Override
	public void deleteBatchByRoleId(String[] roleIds) {
		sysUserRoleDao.deleteBatchByRoleId(roleIds);
	}

	@Override
	public List<String> findByRoleId(String roleId) {
		return sysUserRoleDao.selectByRoleId(roleId);
	}

	@Override
	public List<String> findRoleIdByUserId(String userId) {
		return sysUserRoleDao.findRoleIdByUserId(userId);
	}
}
